package com.socio.model;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class User implements Serializable {

	private static final long serialVersionUID = -8071184133078945086L;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent(mappedBy="user")
	@Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="startTime desc"))
	private Set<Day> days = new LinkedHashSet<Day>(0);
	
	@Persistent()
	private String androidUDID = "";
	
	@Persistent
	private String facebookID = "";
	
	@Persistent
	private String nickname = "";
	
	@Persistent
	private long quickScore;
	
	@Persistent
	private Long facebookQuickScore;
	
	public User(ClientUser user) {
		this.androidUDID = user.getAndroidUDID();
		this.nickname = user.getNickname();
		this.facebookID = user.getFacebookID();
	}
	
	public User() {
	}
	
	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public Set<Day> getDays() {
		return days;
	}

	public void setDays(Set<Day> days) {
		this.days = days;
	}

	public String getAndroidUDID() {
		return androidUDID;
	}

	public void setAndroidUDID(String androidUDID) {
		this.androidUDID = androidUDID;
	}

	public String getFacebookID() {
		return facebookID;
	}

	public void setFacebookID(String facebookID) {
		this.facebookID = facebookID;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public long getQuickScore() {
		return quickScore;
	}

	public void setQuickScore(long quickScore) {
		this.quickScore = quickScore;
	}
	
	public void incrementQuickScore() {
		this.quickScore++;
	}
	
	public Long getFacebookQuickScore() {
		return facebookQuickScore;
	}

	public void setFacebookQuickScore(Long facebookQuickScore) {
		this.facebookQuickScore = facebookQuickScore;
	}
	
	public void incrementFacebookQuickScore() {
		if(facebookQuickScore == null)
			facebookQuickScore = 0L;
		facebookQuickScore++;
	}

	public ClientUser getClientUser() {
		ClientUser clientUser = new ClientUser();
		clientUser.setKey(KeyFactory.keyToString(key));
		clientUser.setNickname(nickname);
		clientUser.setFacebookID(facebookID);
		clientUser.setAndroidUDID(androidUDID);
		clientUser.setQuickScore(quickScore);
		if(facebookQuickScore != null)
			clientUser.setFacebookQuickScore(facebookQuickScore);		
		
		return clientUser;
	}
}
